package websimulator;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import calculations.Moduls;

import buildings.House;

import readwrite.Connector;

/**
 * Servlet implementation class EditModul
 */
@WebServlet("/EditModul")
public class EditModul extends HttpServlet {
	private static final long serialVersionUID = 1L;
       private Connector con;
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public EditModul() throws Exception {
        super();
        con = new Connector();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String answer="";
		int i=0;
		String id="";
		ArrayList<Moduls> moduls;
		try {
			moduls = con.getAllModuls();
			answer += "<div id=\"list\">";
			answer += "<h3>Choose the module you want to edit:</h3>";
			if(moduls != null)
			{
			answer +=		"<table> <tr> <td><h4>Name:</h4> </td> <td><h4>Choose:</h4> </td></tr>";
			for(Moduls m: moduls)
			{
				id = "modulId"+i;
				answer += "<tr> <td> "+m.getName()+" </td>  <td><input type=\"button\" name=\"pick\" id=\"pick\" value=\"Select\" onclick=\"pick('"+i+"')\"></td></tr>";
				answer += "<input type=\"hidden\" name=\"id\" id=\""+id+"\" value=\""+m.getId()+"\">";
				i++;
			}
			}
			answer += "</table></div>";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		response.setContentType("text/plain");
		response.getWriter().write(answer);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String answer="";
		String name="";
		String directive = "";
		String typeText="";
		int modulId = Integer.parseInt(request.getParameter("modulId"));
		String newName = request.getParameter("name");
		int i=0;
		String id="";
		ArrayList<House> houses;
		try {
			if(newName != name && newName != null)
			{
				con.ChangeModulName(modulId, newName);
				name = newName;
				
			}
			else
				name = con.getModulName(modulId);
			
			houses = con.getHousesByModulID(modulId);
			answer += "<div id=\"list\">";
			
			if(houses != null)
			{
				answer += "<input type=\"text\" id=\"name\" value=\""+name+"\">";
				answer += "<input type=\"button\" value=\"Change name\" onclick=\"changeName()\">";
				answer +=		"<table> <tr> <td><h4>People/Employees:</h4></td> <td><h4>Area: </h4></td> <td><h4>Directiv: </h4></td> <td><h4>WallHeight: </h4></td> <td><h4>Type:</h4> </td><td><h4>Delete:</h4></td> </tr>";
				for(House h: houses)
				{
					id = "houseId"+i;
					typeText = con.GetHouseTypeName(h.getBuildingType());
					directive = con.GetDirectivName(h.getDirective());
					answer +=	"<tr> <td> "+h.getPeople()+" </td>  <td> "+h.getArea()+" m^2"+" </td> <td> "+directive+" </td> " +
						" <td> "+h.getWallHeight()+" m"+" </td> <td> "+typeText+" </td> <td><input type=\"button\" name=\"add\" id=\"add\" value=\"Delete\" onclick=\"remove('"+i+"')\";updateList()></td></tr>";
						
					answer += "<input type=\"hidden\" name=\"id\" id=\""+id+"\" value=\""+h.getId()+"\">";
					i++;
				}	
				answer += "</table> "+
						"<input type=\"button\" value=\"Update list\" onclick=\"updateList()\">";
			}
			
				answer += "<input type=\"hidden\" name=\"id\" id=\"modulId\" value=\""+modulId+"\"></div>";
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		response.setContentType("text/plain");
		response.getWriter().write(answer);
		
		
	}

}
